package action

import (
	"time"
	cache "hub.hddznet.com/uniplatform/framework/storage/cache/cache_leveldb"
	"hub.hddznet.com/uniplatform/framework/net/web/jwt"
	"hub.hddznet.com/app/framework/tools"
	"github.com/gin-gonic/gin"
)

type ojwt struct {
	*jwt.GinJWT
}

// 将token解析，并且返回一个map[string]interface{}
func (this *ojwt) Auth(callback func(ctx *gin.Context, token map[string]interface{})) gin.HandlerFunc {
	if this == nil {
		panic("当前jwt没有初始化...")
	}
	return this.GinJWT.Auth(func(ctx *gin.Context, token *jwt.Token) {
		callback(ctx, tools.MapByInterface(token.Claims))
	})
}

var JWT *ojwt

// 初始化Jwt
func InitJwt(cache_file, secret string, expiration time.Duration) {
	cacher := cache.NewLevelCacheOpenFile(cache_file)
	au := jwt.NewGinJWT(expiration, secret, cacher)
	//初始化jwt
	JWT = new(ojwt)
	JWT.GinJWT = au
}
